//
//  HEHealthHisCell.m
//  HealthFamily
//
//  Created by 吴欧 on 2020/12/28.
//  Copyright © 2020 honglutec. All rights reserved.
//

#import "HEHealthHisCell.h"
#import "HEHealthHisItemCell.h"
#import "UICollectionViewLeftAlignedLayout.h"
#import "HEHealthHisItems.h"

@interface HEHealthHisCell ()<UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) UICollectionViewLeftAlignedLayout *layout;
@property (nonatomic, assign) BOOL hasLoad;
@property (nonatomic, strong) NSArray <HEHealthHisItemModel *> *showList;
@property (nonatomic, strong) HEHealthHisItems *model;
@property(nonatomic, strong) QMUITextView *textView;

@property (nonatomic, strong) UILabel *titleLb;

@end
 
@implementation HEHealthHisCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self initUI];
    }
    return self;
}

- (void)initUI {
    
    self.contentView.backgroundColor = KWhiteColor;
    UIView *topLine = [UIView new];
    topLine.backgroundColor = CSplitLineColor;
    [self.contentView addSubview: topLine];

    _titleLb = [UILabel new];
    _titleLb.text = @"-";
    _titleLb.font = DYFont(18);
    _titleLb.textColor = CNavLightBlackColor;
    [self.contentView addSubview: _titleLb];
    
    // 1.创建流水布局
    _layout = [[UICollectionViewLeftAlignedLayout alloc] init];
//    _layout.estimatedItemSize = CGSizeMake(120, 100);
 
    _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:_layout];
    _collectionView.backgroundColor = KWhiteColor;
    _collectionView.dataSource = self;
    _collectionView.delegate = self;
    _collectionView.showsHorizontalScrollIndicator = NO;
    _collectionView.alwaysBounceVertical = YES;
    [self.contentView addSubview:_collectionView];
    
    [_collectionView registerClass:HEHealthHisItemCell.class forCellWithReuseIdentifier:KHEHealthHisItemCell];
    
    _textView = [QMUITextView new];
    // 限制可输入的字符长度
    self.textView.maximumTextLength = 100;
    self.textView.placeholder = @"请详细描述您遇到的问题~";
    self.textView.placeholderColor = UIColorPlaceholder; // 自定义 placeholder 的颜色
    self.textView.textContainerInset = UIEdgeInsetsMake(5, 7, 5, 7);
    self.textView.typingAttributes = @{NSFontAttributeName: UIFontMake(16),
                                       NSParagraphStyleAttributeName: [NSMutableParagraphStyle qmui_paragraphStyleWithLineHeight:20]
    };
    _textView.backgroundColor = CSplitLineColor;
    [self.contentView addSubview:_textView];
    self.textView.layer.cornerRadius = 4;
    [_textView setDelegate:self];

    [topLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).mas_offset(15);
        make.left.equalTo(self.contentView).mas_offset(0);
        make.right.equalTo(self.contentView).mas_offset(-0);
        make.height.offset(10);
    }];
    
    [_titleLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(topLine.mas_bottom).mas_offset(15);
        make.left.equalTo(self.contentView).mas_offset(15);
        make.right.equalTo(self.contentView).mas_offset(-15);
    }];
    
    [_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_titleLb.mas_bottom).mas_offset(10);
        make.left.right.equalTo(self.contentView);
    }];
    
    [_textView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_collectionView.mas_bottom).mas_offset(10);
        make.left.equalTo(self.contentView).mas_offset(15);
        make.right.equalTo(self.contentView).mas_offset(-15);
        make.height.mas_equalTo(100);
        make.bottom.equalTo(self.contentView.mas_bottom).mas_offset(-10);
    }];
}

- (void)configCellWithModel:(HEHealthHisItems *)model {
    _model = model;
    _showList = _model.items;
    _titleLb.text = model.title;
    _textView.placeholder = model.placeHolde;
    _textView.text = model.content;
    
    [_collectionView reloadData];
    [_collectionView layoutIfNeeded];
    
    CGFloat height = [model getCollectionHeight:_collectionView];
    [_collectionView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(@(height)).priorityMedium();
    }];
}

 
//解决ios8上自动布局的问题
- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds{
    return YES;
}


#pragma mark - 代理方法 Delegate Methods
// 设置分区

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

// 每个分区上得元素个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _showList.count;
}

// 设置cell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    HEHealthHisItemCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:KHEHealthHisItemCell forIndexPath:indexPath];
    cell.model = _showList[indexPath.row];
     return cell;
}

// 设置cell大小 itemSize：可以给每一个cell指定不同的尺寸
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    NSString *string = nil;
    HEHealthHisItemModel *model = _showList[indexPath.item];
    string = model.title;

    CGSize size = [string boundingRectWithSize:CGSizeMake(MAXFLOAT, 20) options: NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12]} context:nil].size;
    
    CGFloat width = size.width + 15 * 2;
    
    if (width >= (KScreenWidth-40)) {
        width = KScreenWidth-40;
    }
    
    CGSize rSize = CGSizeMake(width, 34);
    
    return rSize;
}


// 设置UIcollectionView整体的内边距（这样item不贴边显示）
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    // 上 左 下 右
    return UIEdgeInsetsMake(0, 15 , 0, 15);
}

// 设置minimumLineSpacing：cell上下之间最小的距离
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return 15;
}

// 设置minimumInteritemSpacing：cell左右之间最小的距离
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return 15;
}

// 选中cell的回调
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    HEHealthHisItemModel *model = _showList[indexPath.item];
    
    if ([model.title isEqualToString:@"无"]) {
        for (HEHealthHisItemModel *model in _showList) {
            if (![model.title isEqualToString:@"无"]) {
                model.isSelect = NO;
            }
        }
        model.isSelect = !model.isSelect;
    }else {
        for (HEHealthHisItemModel *model in _showList) {
            if ([model.title isEqualToString:@"无"]) {
                model.isSelect = NO;
            }
        }
        model.isSelect = !model.isSelect;
    }
    [_collectionView reloadData];
}

- (void)resetChoose {
    for (HEHealthHisItemModel *model in _showList) {
        model.isSelect = NO;
    }
}

- (void)textViewDidChange:(UITextView *)textView {
    _model.content = textView.text;
}

@end
